package com.zzty.wja.controller;

import com.zzty.wja.entity.User;
import com.zzty.wja.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //验证登录信息
    @RequestMapping(value="/checkLogin",method = RequestMethod.POST)
    public Map checkLogin(@RequestBody User user, HttpServletRequest request){
        Map<String,String> result=new HashMap<>();
        String resultValue="false1";
        User user1=userService.getUserByName(user);
        if(user1!=null){
            if(!user.getPassword().equals(user1.getPassword())){
                resultValue="false2";
            }
            else{
                HttpSession session = request.getSession();
                session.setAttribute("user", user1);
                resultValue=user1.getType();
            }
        }
        result.put("msg",resultValue);
        return result;
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public String addUser(@RequestBody User user){
        String result="success";
        User user1=userService.getUserByName(user);
        if(user1!=null){
            result="exist";
        }
        else{
            userService.addUser(user);
        }
        return result;
    }

    @RequestMapping(value = "/checkUserName",method = RequestMethod.POST)
    public String checkUserName(User user){
        String result="success";
        User user1=userService.getUserByName(user);
        if(user1!=null&&user1.getId()!=user.getId()){
            result="false";
        }
        return result;
    }

    @RequestMapping(value = "/updateUserById",method = RequestMethod.POST)
    public String updateUserById(User user){
        String result="false";
        Integer count=userService.updateUserById(user);
        if(count==1){
            result="success";
        }
        return result;
    }

}
